//
//  BIDNoticeDetailViewController.m
//  cxgov
//
//  Created by mal on 16/3/6.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDLeaveDetailViewController.h"
#import "BIDCreateLeaveViewController.h"
#import "BIDCustomTextView.h"

//批复
static NSString *replyLeaveURL = @"leaveAndroid!modifyLeaveReadStatus.action";
//删除
static NSString *deleteLeaveURL = @"leaveAndroid!dropLeaveReply.action";
//查看请假申请详情
static NSString *leaveDetailURL = @"leaveAndroid!toReadRecLeave.action";

@interface BIDLeaveDetailViewController ()<UIAlertViewDelegate>
{
}
@end

@implementation BIDLeaveDetailViewController
@synthesize leaveId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"请假申请";
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteLeave)];
    //
    _replyTextView.layer.borderColor = RGBACOLOR(182.f, 218.f, 235.f, 1.f).CGColor;
    _replyTextView.layer.cornerRadius = 3.f;
    _replyTextView.layer.borderWidth = 0.5f;
    _replyTextView.clipsToBounds = YES;
    _replyTextView.placeholder = @"请填写您的意见";
    _replyTextView.inputAccessoryView = self.toolBar;
    //
    _agreeBtn.layer.borderColor = RGBACOLOR(43.f, 168.f, 26.f, 1.f).CGColor;
    _agreeBtn.layer.borderWidth = 0.5f;
    _agreeBtn.layer.cornerRadius = 3.f;
    
    _disagreeBtn.layer.borderColor = RGBACOLOR(241.f, 131.f, 73.f, 1.f).CGColor;
    _disagreeBtn.layer.borderWidth = 0.5f;
    _disagreeBtn.layer.cornerRadius = 3.f;
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
}
//加载申请详情
- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], leaveDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", leaveId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSString *strCreatorName = responseDic[@"trueName"];
            NSString *strCreateDate = responseDic[@"createTime"];
            NSString *strNoticeContent = responseDic[@"title"];
            _nameLabel.text = strCreatorName;
            _dateLabel.text = strCreateDate;
            _contentTextView.text = strNoticeContent;
            //
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            CGFloat fontSize = [AppDelegate getFontSize];
            _contentTextView.font = [UIFont systemFontOfSize:fontSize];
            CGFloat contentHeight = [BIDGlobalMethods getHeightWithString:strNoticeContent font:[UIFont systemFontOfSize:fontSize] constraintSize:CGSizeMake(CGRectGetWidth(self.view.frame)-8*2, MAXFLOAT)];
            if(contentHeight>CGRectGetHeight(_contentTextView.frame))
            {
                CGFloat viewHeight = _heightForContainerViewConstraint.constant + (contentHeight-CGRectGetHeight(_contentTextView.frame));
                CGFloat bottomHeight = screenSize.height-64-CGRectGetMinY(_replyTextView.frame);
                if((viewHeight+30)>screenSize.height-bottomHeight-64) viewHeight = screenSize.height-64-30-bottomHeight;
                _heightForContainerViewConstraint.constant = viewHeight;
                [self.view layoutIfNeeded];
            }
        }
    }];
}

//删除
- (void)deleteLeave
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteLeaveURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", leaveId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"删除成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

- (IBAction)agreeBtnHandler:(id)sender
{
    [self replyWithOptionType:2];
}

- (IBAction)disagreeBtnHandler:(id)sender
{
    [self replyWithOptionType:3];
}

//批复
- (void)replyWithOptionType:(NSInteger)type
{
    if(_replyTextView.text.length == 0)
    {
        [BIDGlobalMethods showAlertView:@"请先填写批复意见" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
        return;
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyLeaveURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&readFlag=%ld&vision=%@", leaveId, type, _replyTextView.text];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"批复成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:2];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"批复失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 2)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if(alertView.tag == 1)
    {
        //删除成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
